package com.wanxi.contrller;

import com.github.tobato.fastdfs.domain.fdfs.MetaData;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.wanxi.result.CommonResult;
import com.wanxi.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Api(description = "文件上传服务")
@RestController
@RequestMapping("/file")
public class FileUploadController {

    public static final String DEFAULT_CHARSET = "UTF-8";

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @ApiOperation(value = "文件上传接口",response = CommonResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType="__file", name = "file",value = "上传的文件", required = true)
    })
    @PostMapping ("/upload")
    @ResponseBody
    public Object UploadFile(MultipartFile file) throws IOException {
        Set<MetaData> mataData = new HashSet<>();
        mataData.add(new MetaData("author", "hbj"));
        mataData.add(new MetaData("description",file.getOriginalFilename()));

        StorePath storePath = fastFileStorageClient.uploadFile(
                file.getInputStream(), file.getSize(),
                FilenameUtils.getExtension(file.getOriginalFilename()),
                null);
        String path = "http://101.133.155.92:8888/"+storePath.getFullPath();
        return path;
    }
}
